package BanyuanClub.Study.Day0304;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * FileInputStream 文件输入流
 */
public class TextFileInputStream {
    public static void main(String[] args) {

        //输出src.txt中的字符
        FileInputStream fis = null;
        try{
            fis = new FileInputStream("src.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (null != fis){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //try-with-resource
        //Closeable接口才可以使用
        try(FileInputStream fis1 =
                    new FileInputStream("src.txt")){

            //读一个字节
//            int r = fis.read();
//            System.out.println((char)r);

            byte[] buf = new byte[8];

            //读多个字节
            fis.read(buf);
            System.out.println(new String(buf));

            //读所有字节 当文件很大时
            int len;//表示每一次读取的实际字节数
            StringBuilder sb = new StringBuilder();
//            fis.skip(1);//跳跃多少个字节
//            fis.mark(100);//标记当前字节
            while ((len = fis.read(buf)) != -1){
                sb.append(new String(buf,0,len));
            }
//            fis.reset();//返回标记字节
            System.out.println(sb);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
